package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadableInstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;

import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public List<StationInfoVO> getNearByStation(Double longitude, Double latitude, Double radius) {
        //GEO Operation
        BoundGeoOperations<String, Serializable> stations = redisTemplate.boundGeoOps("stations");
        //Redis HashOperation(选项)
        HashOperations<String, Object, Object> redisHashOperations = redisTemplate.opsForHash();
        Point point = new Point(longitude,latitude);
        Distance distance = new Distance(radius, Metrics.KILOMETERS);
        Circle circle = new Circle(point, distance);
        //设置参数
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        //设置redis返回距离
        args.includeDistance();
        //设置redis返回坐标,经纬度
        args.includeCoordinates();

        GeoResults<RedisGeoCommands.GeoLocation<Serializable>> results = stations.radius(circle,args);
        log.debug("通过经度:{} 纬度:{}距离:{} 结果:{}",longitude,latitude,distance,results);

        //循环遍历返回的结果
        List<StationInfoVO> resultVos = new ArrayList<>();
        for (GeoResult<RedisGeoCommands.GeoLocation<Serializable>> result : results) {
            //获取站的id
            Serializable name = result.getContent().getName();
            //获取站和我的位置的距离
            double stationDistance = result.getDistance().getValue();
            //获取站的经纬度
            Point stationPoint = result.getContent().getPoint();
            Integer stationId = Integer.valueOf(String.valueOf(name));
            String bigKey = String.valueOf(name);
            //通过大key 站点id 获取小map所有的值
            Map<Object, Object> entries = redisHashOperations.entries(bigKey);
            log.debug("通过大key:{}获取小map里所有的值:{}",name,entries);
            Object deviceNumberObj = entries.get("deviceNumber");
            Object codeObj = entries.get("code");
            Object nameObj = entries.get("name");
            Object stationIdObj = entries.get("stationId");
            Object OperatorObj = entries.get("operatorId");
            Object IdObj = entries.get("Id");
            
            //封装返回结果对象并且设置信息
            StationInfoVO stationInfoVO = new StationInfoVO();
            //设置距离
            stationInfoVO.setDistance(stationDistance);
            stationInfoVO.setStationId(Integer.parseInt(String.valueOf(stationIdObj)));
            stationInfoVO.setOperatorId(Long.parseLong(String.valueOf(OperatorObj)));
            stationInfoVO.setStationId(stationId);
            stationInfoVO.setId(Integer.parseInt(String.valueOf(IdObj)));
            stationInfoVO.setStationCode(String.valueOf(codeObj));
            stationInfoVO.setStationName(String.valueOf(nameObj));
            stationInfoVO.setDeviceNumber(Integer.valueOf(String.valueOf(deviceNumberObj)));
            //设置经度
            stationInfoVO.setLongitude(stationPoint.getX());
            //设置维度
            stationInfoVO.setLatitude(stationPoint.getY());
            resultVos.add(stationInfoVO);
            return resultVos;
        }

        return Collections.emptyList();
    }
}
